package com.mark.springbootredis.controller;

import com.mark.springbootredis.common.Result;
import com.mark.springbootredis.request.LoginRequest;
import com.mark.springbootredis.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: MARK
 * @Date: 2019/8/20 11:24
 * @Version: 1.0.0
 * @Description:
 */
@RestController
@RequestMapping(value = "/admin", produces = {"application/json;charset=UTF-8"})
@CrossOrigin
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("login")
    public Result login(@RequestBody LoginRequest loginRequest, HttpServletRequest request) {
        if (adminService.login(loginRequest)) {
            //存到redis中
            String username = loginRequest.getUsername();
            Object result = stringRedisTemplate.opsForValue().get("username-"+username);
            if (result == null) {
                log.info("该用户正在存到redis中");
                stringRedisTemplate.opsForValue().set("username-"+username, username);
                log.info("redis存储结束");
            }
            return Result.ok("登录成功");
        }
        return Result.fail("登录失败");
    }

}
